package com.example.androidproject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import android.app.Activity;
//import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
//import android.widget.Toast;

public class ScheduleActivity extends Activity implements OnClickListener {
	int currentWeek = 1;

	ArrayList<String> groups = new ArrayList<String>();
	
	ArrayList<String> coursesMonday = new ArrayList<String>();
	ArrayList<String> coursesTuesday = new ArrayList<String>();
	ArrayList<String> coursesWednesday = new ArrayList<String>();
	ArrayList<String> coursesThursday = new ArrayList<String>();
	ArrayList<String> coursesFriday = new ArrayList<String>();
	ArrayList<ArrayList<String>> daysOfWeek = new ArrayList<ArrayList<String>>();

	
	ArrayList<Map<String, String>> groupData;

	ArrayList<Map<String, String>> childDataItem;

	ArrayList<ArrayList<Map<String, String>>> childData;

	Map<String, String> m;

	ExpandableListView elvMain;
	TextView weekIndicator;
	Button next;
	Button prev;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_schedule, menu);
        return true;
    }
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule);
		
		populateThisWeeksSchedule();

		groupData = new ArrayList<Map<String, String>>();
		
		
		for (String group : groups) {
			m = new HashMap<String, String>();
			m.put("groupName", group);
			groupData.add(m);
		}
		
		
		String groupFrom[] = new String[] { "groupName" };

		int groupTo[] = new int[] { android.R.id.text1 };

		childData = new ArrayList<ArrayList<Map<String, String>>>();

		childDataItem = new ArrayList<Map<String, String>>();

		for (ArrayList<String> thisDayCourses : daysOfWeek) {
			childDataItem = new ArrayList<Map<String, String>>();
			for (String course : thisDayCourses) {
				m = new HashMap<String, String>();
				m.put("courseName", course);
				childDataItem.add(m);
			}
			childData.add(childDataItem);
		}

		
		
		String childFrom[] = new String[] { "courseName" };

		int childTo[] = new int[] { android.R.id.text1 };

		SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
				this, groupData,
				android.R.layout.simple_expandable_list_item_1, groupFrom,
				groupTo, childData, android.R.layout.simple_list_item_1,
				childFrom, childTo);

		elvMain = (ExpandableListView) findViewById(R.id.elvMain);
		elvMain.setAdapter(adapter);
		
		weekIndicator = (TextView) findViewById(R.id.tvWeek);
		weekIndicator.setText("Week " + currentWeek);
		
		next = (Button) findViewById(R.id.btnNext);
		next.setOnClickListener(this);
		
		prev = (Button) findViewById(R.id.btnPrev);
		prev.setOnClickListener(this);
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnNext:
			if(currentWeek > 15) {
				next.setClickable(false);
				break;
			}
			prev.setClickable(true);
			nextWeek();
			break;
			
		case R.id.btnPrev:
			if(currentWeek < 2) {
				prev.setClickable(false);
				break;
			}
			next.setClickable(true);
			prevWeek();
			break;
		}
		weekIndicator.setText("Week " + currentWeek);
	}
	
	private void prevWeek() {
		currentWeek--;
		populateThisWeeksSchedule();
	}

	private void nextWeek() {
		currentWeek++;
		populateThisWeeksSchedule();
	}

	private void populateThisWeeksSchedule() {
		/* 
		 * 
		 * Tut tipa nabivaem nedelju lekcijami (iz gugla koroche)
		 * Berjom koro4e s googla info na nuzhnuju nedelju - hz kak tam eto delat'.
		 * 
		 * Dalee - bolvanka: 
		 * 
		 * (warning: high shit-code levels detected)
		 * 
		 * 
		 */
		
		coursesMonday.add("Math");
		coursesMonday.add("Geometry");
		coursesMonday.add("Algebra");
		coursesMonday.add("Physics");
		if(!coursesMonday.isEmpty()) {
			daysOfWeek.add(coursesMonday);
			groups.add("Monday");
		}
		
		coursesTuesday.add("English");
		coursesTuesday.add("French");
		coursesTuesday.add("German");
		if(!coursesTuesday.isEmpty()) {
			daysOfWeek.add(coursesTuesday);
			groups.add("Tuesday");
		}
		
		coursesWednesday.add("Sports");
		coursesWednesday.add("Gym");
		coursesWednesday.add("Football");
		coursesWednesday.add("Bicycle");
		if(!coursesWednesday.isEmpty()) {
			daysOfWeek.add(coursesWednesday);
			groups.add("Wednesday");
		}
		
		coursesThursday.add("Vocal");
		coursesThursday.add("Guitar");
		if(!coursesThursday.isEmpty()) {
			daysOfWeek.add(coursesThursday);
			groups.add("Thursday");
		}
		
		coursesFriday.add("Nothing");
		coursesFriday.add("Really");
		if(!coursesFriday.isEmpty()) {
			daysOfWeek.add(coursesFriday);
			groups.add("Friday");
		}
	}
}
